﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using SchemaExplorer;

public class GeneHelper
{
    public GeneHelper()
    {

    }

    /// <summary>
    /// 根据表名转化为类名
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="prefix"> </param>
    /// <param name="suffix"> </param>
    /// <returns></returns>
    public static string GetClassNmByTbNm(string tableName, string prefix, string suffix)
    {
        string newNm = string.Empty;

        string[] nmChild = tableName.Split('_');

        //移出前缀
        if (!string.IsNullOrEmpty(prefix) && nmChild[0] == prefix)
        {
            string[] temp = new string[nmChild.Length - 1];
            Array.Copy(nmChild, 1, temp, 0, temp.Length);
            nmChild = temp;
        }

        //移出后缀
        if (!string.IsNullOrEmpty(suffix) && nmChild[nmChild.Length - 1] == suffix)
        {
            string[] temp = new string[nmChild.Length - 1];
            Array.Copy(nmChild, 0, temp, 0, temp.Length);
            nmChild = temp;
        }

        for (int i = 0; i < nmChild.Length; i++)
        {
            newNm += FirstCharToUppper(nmChild[i]);
        }
        return newNm;
    }

    /// <summary>
    /// 使字符串的首字母大写
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string FirstCharToUppper(string str)
    {
        string firstChar = str.Substring(0, 1);//首字母
        string otherStr = str.Substring(1, str.Length - 1);
        return firstChar.ToUpper() + otherStr;
    }

    public static string GetJavaType(DbType dbType, bool allowDbNul)
    {
        switch (dbType)
        {
            case DbType.StringFixedLength:
            case DbType.String:
            case DbType.AnsiString:
            case DbType.AnsiStringFixedLength:
            case DbType.Binary:
                return "String";
            case DbType.Int16:
            case DbType.Int32:
            case DbType.UInt16:
            case DbType.Single:
            case DbType.Byte:
                return "int";
            case DbType.Boolean:
                return "boolean";
            case DbType.UInt32:
            case DbType.Int64:
            case DbType.Decimal:
            case DbType.Double:
                return "double";
            case DbType.Date:
            case DbType.DateTime:
                return "Date";
            default:
                throw new NotImplementedException("类型[" + dbType.ToString() + "]还未对应!");
        }
    }

    private static string AppeadNull(string type, bool allowDbNul)
    {
        if (!allowDbNul)
        {
            return type;
        }
        return type + "?";
    }

    /// <summary>
    /// 根据数据库表中列字段的名字
    /// </summary>
    /// <param name="field"></param>
    /// <returns></returns>
    public static string GetPropNm(string fieldNm)
    {
        string[] nmChild = fieldNm.Split('_');
        return GetNewNm(nmChild);
    }

    /// <summary>
    /// 根据数据库表中列字段的名字
    /// </summary>
    /// <param name="field"></param>
    /// <returns></returns>
    public static string GetFieldNm(string fieldNm)
    {
        string[] nmChild = fieldNm.Split('_');
        if (nmChild.Length == 1)
        {
            return nmChild[0];
        }
        string[] nmChild2 = new string[nmChild.Length - 1];

        Array.Copy(nmChild, 1, nmChild2, 0, nmChild.Length - 1);

        return nmChild[0] + GetNewNm(nmChild2);
    }

    /// <summary>
    /// 通过一些子的名字去获取
    /// </summary>
    /// <param name="childNm"></param>
    /// <returns></returns>
    public static string GetNewNm(string[] childNm)
    {
        string newNm = string.Empty;
        for (int i = 0; i < childNm.Length; i++)
        {
            newNm += FirstCharToUppper(childNm[i]);
        }
        return newNm;
    }
}
